package io.renren.modules.sys.service.impl;


import io.renren.common.utils.ImportExcelTools;
import io.renren.common.utils.TimeUtils;
import io.renren.modules.sys.entity.ExcelVo;
import io.renren.modules.sys.entity.SignInLogDto;
import io.renren.modules.sys.entity.SignInLogEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.SignInLogDao;

import io.renren.modules.sys.service.SignInLogService;
import org.springframework.transaction.annotation.Transactional;


@Service("signInLogService")
public class SignInLogServiceImpl extends ServiceImpl<SignInLogDao, SignInLogEntity> implements SignInLogService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SignInLogEntity> page = this.page(
                new Query<SignInLogEntity>().getPage(params),
                new QueryWrapper<SignInLogEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public List<SignInLogEntity> queryPageS(Map<String, Object> params) {

        String checkInCard = (String) params.get("checkInCard");
        String[] split = checkInCard.split(",");

        List<SignInLogEntity> list = new ArrayList<>();

        for (String s : split) {
            QueryWrapper<SignInLogEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("check_in_card" , s);
            List<SignInLogEntity> list1 = this.list(wrapper);
            list.addAll(list1);
        }
        List<SignInLogEntity> collect = list.stream().sorted(Comparator.comparing(SignInLogEntity::getCheckInTime).reversed()).collect(Collectors.toList());


        return collect;
    }


    @Transactional
    @Override
    public boolean manualImportReturnBoolean(ExcelVo excelVo) {
        if (excelVo == null || StringUtils.isBlank(excelVo.getPath())) {
            log.error("导入失败,请排查入参:"+ excelVo.toString());
            return false;
        }

        List<SignInLogDto> signInLogDtoList = new ArrayList<>();
        List<SignInLogEntity> signInLogList = new ArrayList<>();

        /** 获取上传文件的流文件 */
        try {

            /** 读取文件流 */
            InputStream is = new FileInputStream(excelVo.getPath());

            /** 执行方法获取到list */
            signInLogDtoList = ImportExcelTools.convertSheetToList(is, SignInLogDto.class, false, excelVo.getStartLine(), excelVo.getMaxCount());

            /** 时间转换,bean复制 **/
            for (SignInLogDto signInLogDto : signInLogDtoList) {
                SignInLogEntity singInLog = new SignInLogEntity();
                BeanUtils.copyProperties(signInLogDto , singInLog , "checkInTime");

                /** 时间转换 **/
                LocalDateTime localDateTime = TimeUtils.stringToLocalDateTime(signInLogDto.getCheckInTime());
                Date date = Date.from(localDateTime.atZone( ZoneId.systemDefault()).toInstant());
                singInLog.setCheckInTime(date);
                signInLogList.add(singInLog);
            }
            boolean saveBatch = true;
            if (signInLogList.size() > 0) {
                /** 批量插入 **/
                this.saveBatch(signInLogList);
            }


            return saveBatch;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            log.error("手动导入异常 , 原因:" + e.getMessage());
            return false;
        }


    }

}